package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.config.cache.TsCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory2;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		// 创建一个空的 用于最后封装数据返回前端
		List<JSONObject> leastList = new ArrayList<>();
		//获取所有分类数据 select * from base_category_view where is_delete = 0
		List<BaseCategoryView> baseCategoryViewList =  baseCategoryViewMapper.selectList(null);
		//设置一个Map集合  存储一级分类的数据
		Map<Long, List<BaseCategoryView>> categoryMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		// 循环遍历集合 得到 category2
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = categoryMap.entrySet().iterator();
		while (iterator1.hasNext()){
			//获取iterator1里面的元素
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator1.next();
			//一级分类ID
			Long category1Id = entry.getKey();
			//获取一级分类名称  因为是重复的数据  所以获取任意一条就行  一般是0  第一条
			String category1Name = entry.getValue().get(0).getCategory1Name();
			// 构建数据 将查询出来的数据塞到一个新的JSONObject里面
			JSONObject caregory1 = new JSONObject();
			caregory1.put("categoryId",category1Id);
			caregory1.put("categoryName",category1Name);

			// 获取二级分类信息  二级分类ID作为key  二级分类数据作为value
			Map<Long, List<BaseCategoryView>> category2Map = entry.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator2 = category2Map.entrySet().iterator();
			// 创建一个集合
			ArrayList<JSONObject> category2Child = new ArrayList<>();
			while (iterator2.hasNext()){
				//获取iterator2里面的元素
				Map.Entry<Long, List<BaseCategoryView>> entry2 = iterator2.next();
				//二级分类ID
				Long category2Id = entry2.getKey();
				//获取二级分类名称  因为是重复的数据  所以获取任意一条就行  一般是0  第一条
				String category2Name = entry2.getValue().get(0).getCategory2Name();
				// 构建数据 将查询出来的数据塞到一个新的JSONObject里面  二级分类对象
				JSONObject caregory2 = new JSONObject();
				caregory2.put("categoryId",category2Id);
				caregory2.put("categoryName",category2Name);

				// 获得三级分类信息  三级分类ID作为key  三级分类数据作为value  并添加到一个集合中
				List<JSONObject> category3Child = entry2.getValue().stream().map(baseCategoryView -> {
					// 构建三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId",baseCategoryView.getCategory3Id());
					category3.put("categoryName",baseCategoryView.getCategory3Name());
					// 数据返回收集
					return category3;
				}).collect(Collectors.toList());
				//三级分类数据添加到二级分类对象中
				caregory2.put("categoryChild",category3Child);
				//将二级对象 添加到集合中
				category2Child.add(caregory2);
			}
			// 将二级对象添加到一级分类对象中
			caregory1.put("categoryChild",category2Child);
			// 将一级对象添加到集合中 返回前端list
			leastList.add(caregory1);
		}
		return leastList;
	}

	@Override
	public List<JSONObject> findAttribute(Long category1Id) {
		//调用mapper层 获得数据
		return baseAttributeMapper.selectAttribute(category1Id);
	}

	@TsCache(prefix = RedisConstant.ALBUM_INFO_PREFIX + "CategoryList:")
	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//创建一个一级分类对象
		JSONObject category1 = new JSONObject();
		category1.put("categoryId",category1Id);
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		category1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		// 二级分类集合 赛到一级分类里面
		// 获取
		Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = category2Map.entrySet().iterator();
		// 创建集合列表
		ArrayList<JSONObject> categoryChild2List = new ArrayList<>();
		while (iterator.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> entry2 = iterator.next();
			Long category2Id = entry2.getKey();
			List<BaseCategoryView> category2List = entry2.getValue();
			JSONObject category2 = new JSONObject();
			category2.put("categoryId",category2Id);
			category2.put("categoryName",category2List.get(0).getCategory2Name());
			// 三级分类集合 塞到二级分类里面
			List<JSONObject> categoryChild3List = baseCategoryViewList.stream().map(baseCategoryView -> {
				JSONObject category3 = new JSONObject();
				category3.put("categoryId", baseCategoryView.getCategory3Id());
				category3.put("categoryName", baseCategoryView.getCategory3Name());
				return category3;
			}).toList();
			category2.put("categoryChild",categoryChild3List);
			categoryChild2List.add(category2);
		}
		category1.put("categoryChild",categoryChild2List);
		return category1;
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		// 查询三级分裂数据
		//先获取二级id
		List<Long> category2IdList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
										.eq(BaseCategory2::getCategory1Id, category1Id)).stream().map(BaseCategory2::getId).toList();
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
				.in(BaseCategory3::getCategory2Id, category2IdList).eq(BaseCategory3::getIsTop, 1).last("limit 7"));
		return baseCategory3List;
	}

	@Override
	public List<BaseCategory1> findAllCategory1() {
		return baseCategory1Mapper.selectList(null);
	}
}
